package com.zzl.bonds.ticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.bonds.ticket.dao.TicketProtocolDao;
import com.zzl.bonds.ticket.dto.TickeprotocolAddDto;
import com.zzl.bonds.ticket.dto.TickeprotocolUpdateDto;
import com.zzl.bonds.ticket.entity.TicketProtocolEntity;
import com.zzl.bonds.ticket.service.TicketProtocolService;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.serurity.entity.User;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 合同模板
 *
 * @author gull
 * @date 2022-02-16 14:04:04
 */
@Service("ticketProtocolService")
public class TicketProtocolServiceImpl extends ServiceImpl<TicketProtocolDao, TicketProtocolEntity> implements TicketProtocolService {

    /**
     * 分页查询
     *
     * @param params 查询参数
     * @return 分页数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TicketProtocolEntity> page = this.page(
                new Query<TicketProtocolEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 新增
     *
     * @param tickeprotocolAddDto 新增实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void saveTickeprotocol(TickeprotocolAddDto tickeprotocolAddDto, User user) throws SysException {
        TicketProtocolEntity tickeprotocolEntity = new TicketProtocolEntity();
        BeanUtils.copyProperties(tickeprotocolAddDto, tickeprotocolEntity);
        tickeprotocolEntity.setCreatorId(user.getId());
        if(!this.save(tickeprotocolEntity)){
            throw new SysException("A00001", "保存失败！");
        }
    }

    /**
     * 修改
     *
     * @param tickeprotocolUpdateDto 修改实体
     * @param user 当前登录用户
     * @throws SysException 业务异常
     */
    @Override
    public void updateTickeprotocolById(TickeprotocolUpdateDto tickeprotocolUpdateDto, User user) throws SysException {
        TicketProtocolEntity tickeprotocolEntity = new TicketProtocolEntity();
        BeanUtils.copyProperties(tickeprotocolUpdateDto, tickeprotocolEntity);
        tickeprotocolEntity.setUpdaterId(user.getId());
        if(!this.updateById(tickeprotocolEntity)){
            throw new SysException("A00002", "修改失败！");
        }
    }

}